package homework;

import java.util.Scanner;

/**
 * @classname:Exception
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/4 18:53
 * @version: 0.1
 * @since:1.8
 */
public class Exception {
    static int[] goods = {5,5,5,5,5};

    public static void main(String[] args) {
        Scanner input  = new Scanner(System.in);

        while (true){
            System.out.println("=======开始购物============");
            System.out.println("请输入商品编号");
            int index= input.nextInt();
            System.out.println("请输入购买商品个数");
            int amount= input.nextInt();

            try {
                sellgood(index,amount);
            } catch (GoodsBuyException e) {
                System.out.println(e.getMessage());
            }
            System.out.println("=======结束购物============");
        }
    }


    public  static void sellgood(int index ,int amount){
        if(index<0||index>goods.length){
            throw  new GoodsBuyException("输入的商品编号不存在");
        }
        if(goods[index]==0){
            throw new GoodsBuyException("商品已经卖光");
        }
        if(amount>goods[index]){
            throw new GoodsBuyException("购买商品的数量超过库存无法购买");
        }
        System.out.println("购买成功");
        goods[index]-=amount;

    }


}
